package com.enqualcomm.kids.view;


import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.activities.FamilyActivity;
import com.enqualcomm.kids.bean.FamilyMember;

import java.util.ArrayList;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;

/**
 * Created by yinzhiqun on 2015/8/7.
 */
public class FamilyListItem extends RelativeLayout implements AdapterView.OnItemLongClickListener {
    private int category;
    private boolean chatEnable;
    private ArrayList<FamilyMember> members;
    private FamilyActivity context;
    private int category_iv_id = R.id.big_icon_iv;
    private String watchName;

    //    public FamilyListItem(FamilyActivity context, ArrayList<FamilyMember> members, boolean chatEnable) {
//        super(context);
//        if (members!=null&&members.size()>0){
//            this.context = context;
//            this.members = members;
//            this.category = members.get(0).category;
//            this.chatEnable = chatEnable;
//            init();
//        }
//    }
    public FamilyListItem(FamilyActivity context, ArrayList<FamilyMember> members, boolean chatEnable, String watchNmae) {
        super(context);
        if (members != null && members.size() > 0) {
            this.context = context;
            this.members = members;
            this.category = members.get(0).category;
            this.chatEnable = chatEnable;
            this.watchName = watchNmae;
            init();
        }
    }

    private void init() {
        int dp16 = DensityUtil.dip2px(context, 16);
        int dp5 = DensityUtil.dip2px(context, 5);
        setPadding(dp16, 0, dp16, dp5);
        initCategoryIv();
        initCategoryTv();
        initListView();
    }

    private void initCategoryIv() {
        ImageView category_iv = new ImageView(context);
        category_iv.setId(category_iv_id);
        category_iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        switch (category) {
            case FamilyMember.CATEGORY_ADMIN:
                category_iv.setImageResource(R.drawable.family_category_admin);
                break;
            case FamilyMember.CATEGORY_BACKUP_ADMIN:
                category_iv.setImageResource(R.drawable.family_category_backup_admin);
                break;
            case FamilyMember.CATEGORY_OTHERS:
                category_iv.setImageResource(R.drawable.family_category_others);
                break;
            case FamilyMember.CATEGORY_FRIEND:
                category_iv.setImageResource(R.drawable.family_category_friend);
                break;
        }

        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = layoutParams.rightMargin = layoutParams.bottomMargin = DensityUtil.dip2px(context, 5);
        addView(category_iv, layoutParams);
    }

    private void initCategoryTv() {
        TextView category_tv = new TextView(context);
        category_tv.setGravity(Gravity.CENTER_HORIZONTAL);
        category_tv.setTextSize(16);
        switch (category) {
            case FamilyMember.CATEGORY_ADMIN:
                category_tv.setTextColor(getResources().getColor(R.color.text1));
                category_tv.setText(R.string.admin);
                if (watchName != null) {
                    MyFrontUtil.initTextViewText(context, category_tv, watchName, context.getString(R.string.admin));
                }
                break;
            case FamilyMember.CATEGORY_BACKUP_ADMIN:
                category_tv.setTextColor(getResources().getColor(R.color.text1));
                category_tv.setText(R.string.backup_admin);
                if (watchName != null) {
                    MyFrontUtil.initTextViewText(context, category_tv, watchName, context.getString(R.string.backup_admin));
                }
                break;
            case FamilyMember.CATEGORY_OTHERS:
                category_tv.setTextColor(getResources().getColor(R.color.text1));
                category_tv.setText(R.string.others);
                if (watchName != null) {
                    MyFrontUtil.initTextViewText(context, category_tv, watchName, context.getString(R.string.others));
                }
                break;
            case FamilyMember.CATEGORY_FRIEND:
                category_tv.setTextColor(getResources().getColor(R.color.text1));
                category_tv.setText(R.string.friend);
                if (watchName != null) {
                    MyFrontUtil.initTextViewText(context, category_tv, watchName, context.getString(R.string.friend));
                }
                break;
        }

        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.RIGHT_OF, category_iv_id);
        layoutParams.topMargin = DensityUtil.dip2px(context, 12);
        addView(category_tv, layoutParams);
    }

    private void initListView() {
        ListView listView = new ListView(context) {
            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                        MeasureSpec.AT_MOST);
                super.onMeasure(widthMeasureSpec, expandSpec);
            }
        };
        listView.setDivider(null);
        listView.setCacheColorHint(Color.TRANSPARENT);
        listView.setBackgroundResource(R.drawable.family_item_bg);
        listView.setAdapter(new FamilyAdapter(members, category, chatEnable, context));
        listView.setOnItemLongClickListener(this);

        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.BELOW, category_iv_id);
        addView(listView, layoutParams);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        context.onLongClick(members.get(i));
        return false;
    }

    static class FamilyAdapter extends BaseAdapter {
        private ArrayList<FamilyMember> members;
        private int category;
        private LayoutInflater inflater;
        private boolean chatEnable;
        private int topMargin;
        private int topMargin2;
        private Context context;

        public FamilyAdapter(ArrayList<FamilyMember> members, int category, boolean chatEnable, Context context) {
            this.members = members;
            this.category = category;
            this.chatEnable = chatEnable;
            this.context = context;
            inflater = LayoutInflater.from(context);
            topMargin = DensityUtil.dip2px(context, 14);
            topMargin2 = DensityUtil.dip2px(context, 24);
        }

        @Override
        public int getCount() {
            return members.size();
        }

        @Override
        public Object getItem(int i) {
            return members.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.family_list_item2, null);
                holder = new ViewHolder();
                holder.display_name_tv = (TextView) convertView.findViewById(R.id.display_name_tv);
                holder.phone_number_tv = (TextView) convertView.findViewById(R.id.phone_number_tv);
                holder.phone_name_tv = (TextView) convertView.findViewById(R.id.phone_name_tv);
                holder.permission_iv = (ImageView) convertView.findViewById(R.id.permission_iv);
//                holder.divider = convertView.findViewById(R.id.divider);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final FamilyMember member = members.get(i);

            holder.phone_name_tv.setText(member.phonename);
            switch (category) {
                case FamilyMember.CATEGORY_ADMIN:
                    holder.permission_iv.setImageResource(chatEnable ? R.drawable.family_permission_1 : R.drawable.family_permission_4);
                    break;
                case FamilyMember.CATEGORY_BACKUP_ADMIN:
                    holder.permission_iv.setImageResource(chatEnable ? R.drawable.family_permission_1 : R.drawable.family_permission_4);
                    break;
                case FamilyMember.CATEGORY_OTHERS:
                    holder.permission_iv.setImageResource(chatEnable ? R.drawable.family_permission_2 : R.drawable.family_permission_5);
                    break;
                case FamilyMember.CATEGORY_FRIEND://不支持微话的手表应该也没有小伙伴
                    holder.permission_iv.setImageResource(R.drawable.family_permission_3);
                    break;
            }

            if (TextUtils.isEmpty(member.displayName)) {//如果没有名字，显示电话号码，并居中

                holder.display_name_tv.setText("");
                holder.phone_number_tv.setText(context.getString(R.string.family_number) + member.phonenumber);   //lw看了ios的需求就改动了，没有一个标准，只有所谓的需求，qnmb的吧
//                holder.display_name_tv.setText(member.phonenumber);
//                holder.phone_number_tv.setText("");
//                LayoutParams layoutParams = (LayoutParams) holder.display_name_tv.getLayoutParams();
//                layoutParams.topMargin = topMargin2;
//                holder.display_name_tv.setLayoutParams(layoutParams);
            } else {
                holder.display_name_tv.setText(member.displayName);
                holder.phone_number_tv.setText(context.getString(R.string.family_number) + member.phonenumber);
//                LayoutParams layoutParams = (LayoutParams) holder.display_name_tv.getLayoutParams();
//                layoutParams.topMargin = topMargin;
//                holder.display_name_tv.setLayoutParams(layoutParams);
            }

            //背景颜色
//            int size = members.size();
//            if (size==1){//只有一个条目
//                holder.divider.setVisibility(View.INVISIBLE);
//            }else if(i==0){//多个条目，最前
//                holder.divider.setVisibility(View.VISIBLE);
//            }else if (i==size-1){//多个条目，最后
//                holder.divider.setVisibility(View.INVISIBLE);
//            }else{//多个条目，中间
//                holder.divider.setVisibility(View.VISIBLE);
//            }
            return convertView;
        }
    }

    static class ViewHolder {
        TextView display_name_tv;
        TextView phone_number_tv;
        TextView phone_name_tv;
        ImageView permission_iv;
        View divider;
    }

    public interface LongClickListener {
        void onLongClick(FamilyMember member);
    }
}
